using codeparser.net.Exceptions;

using NUnit.Framework;

using System;

namespace codeparser.net.UnitTests
{
    /// <summary>
    /// Contains the tests for the Parser class.
    /// </summary>
    [TestFixture]
    public class ParserTest
    {
        /// <summary>
        /// Contains the parser object.
        /// </summary>
        private Parser _parser;

        /// <summary>
        /// Initializes this instance.
        /// </summary>
        [TestFixtureSetUp]
        public void Initialize()
        {
            // Load the parser configuration.
            ParserConfiguration configuration = new ParserConfiguration("Configuration.xml");

            // Initialize the parser.
            this._parser = new Parser(configuration);
        }

        /// <summary>
        /// Tests parsing a simple tag.
        /// </summary>
        [Test]
        public void ParseSimpleTag()
        {
            string input = "[b]Test[/b]";
            string expected = "<strong>Test</strong>";

            string actual = _parser.Parse(input);

            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        /// Tests parsing a url tag.
        /// </summary>
        [Test]
        public void ParseUrlTag()
        {
            string input = "[url]http://www.codeparser.net[/url]";
            string expected = "<a href=\"http://www.codeparser.net\">http://www.codeparser.net</a>";

            string actual = _parser.Parse(input);

            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        /// Tests parsing nested simple tags.
        /// </summary>
        [Test]
        public void ParseNestedSimpleTags()
        {
            string input = "[b]Dies ist [i]nur[/i] ein Test[/b].";
            string expected = "<strong>Dies ist <em>nur</em> ein Test</strong>.";

            string actual = _parser.Parse(input);

            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        /// Tests invalidly nested simple tags.
        /// </summary>
        [Test]
        [ExpectedException(typeof(InvalidTagNestingException))]
        public void ParseInvalidlyNestedSimpleTags()
        {
            string input = "[b]Dies ist [i]nur[/b] ein Test[/i].";

            string actual = _parser.Parse(input);
        }

        /// <summary>
        /// Tests parsing with parameters.
        /// </summary>
        [Test]
        public void ParseTagWithParameter()
        {
            string input = "[url=http://www.spiegel.de]Test[/url].";
            string expected = "<a href=\"http://www.spiegel.de\">Test</a>.";

            string actual = _parser.Parse(input);

            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        /// Tests parsing of special characters.
        /// </summary>
        [Test]
        public void ParseSpecialCharacter()
        {
            string input = "Dies ist [[ein] Tag.";
            string expected = "Dies ist [ein] Tag.";

            string actual = _parser.Parse(input);

            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        /// Tests parsing of special character contained within a simple tag.
        /// </summary>
        [Test]
        public void ParseSpecialCharacterNestedWithSimpleTag()
        {
            string input = "Dies [b]ist [[ein] Tag[/b].";
            string expected = "Dies <strong>ist [ein] Tag</strong>.";

            string actual = _parser.Parse(input);

            Assert.AreEqual(expected, actual);
        }
    }
}